<?php if (!defined('THINK_PATH')) exit();?><style type="text/css">
    .add_label {
        display: -moz-inline-box;
        display: inline-block;
        width: 60px;
        text-align: right;
    }

    #dd div {
        padding-right: 10px;
        padding-bottom: 10px;
    }

    .add_table {
        width: 100%;
    }

    .add_table th {
        width: 140px;
        background-color: #E0ECFF;
        padding: 8px 0 8px 0;
        border-right: solid 1px #95B8E7;
        border-bottom: solid 1px #95B8E7;
    }

    .add_table td {
        border-bottom: solid 1px #95B8E7;
    }

    .add_table tr {
        border: solid 1px #95B8E7;
    }

    .inline {
        display: inline
    }

    .row_container {
        padding: 8px 5px 5px 5px;
    }
    #syfz-dg{
        width: 100%;
        height: 300px;
    }
</style>
<!-- 工具栏 -->
<div style="background-color: #E0ECFF;">
    <table>
        <tr>
            <td>
                <a href="#"  class="easyui-linkbutton" onclick='submit_zds("save")'
                data-options="iconCls:'icon-ok',plain:true">保存</a>
                <a href="#"  class="easyui-linkbutton" onclick='submit_zds("will_check")'
                data-options="iconCls:'icon-ok',plain:true">发布</a>
                <a href="#" id= "add_lsgd" class="easyui-linkbutton"  onclick="FullScrean()" data-options="iconCls:'icon-standard-monitor',plain:true">全屏</a>
                <a href="#"  class="easyui-linkbutton" onclick='go_back_zds("<?php echo ($data[id]); ?>")'
                data-options="iconCls:'icon-back',plain:true">返回</a>
                <!--<a href="#"  class="easyui-linkbutton" onclick='testtt()'-->
                   <!--data-options="iconCls:'icon-back',plain:true">测试</a>-->

            </td>
        </tr>
    </table>
</div>
<div id="cc" class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west',title:'West',title:'表头信息',split:true,hideCollapsedContent:false" style="width:550px;">

        <form id="add_zds_form" name="myform" enctype="multipart/form-data" method="post">
            <table class="add_table">
                <tr>

                    <td>
                        <div class="row_container">

                            <div class="inline">
                                <span class="add_label">标题：</span>
                                <input id="add_title" name="title" class="easyui-textbox"
                                       data-options="iconCls:'',multiline:true,editable:true,required:true"
                                       style="width:360px;height:80px;" value='<?php echo ($data["title"]); ?>'/>
                            </div>

                        </div>


                    </td>
                </tr>
                <tr>

                    <td>
                        <div class="row_container">
                            <div class="inline">
                                <span class="add_label">车间：</span>
                                <input id="cj_add" name="cj" class="easyui-textbox"
                                       data-options="iconCls:'',editable:true,required:true,panelHeight:'auto'"
                                       style="width:100px" value='<?php echo ($data["cj"]); ?>'/>
                            </div>
                            <div class="inline">
                                <span class="add_label">工位：</span>
                                <input id="gw_add" class="easyui-combogrid" data-options="editable:false,required:true"
                                       name="gw[]" style="width:200px;" value='<?php echo ($data["gw"]); ?>'/>


                            </div>


                        </div>
                    </td>
                </tr>
                <tr>

                    <td>
                        <div class="row_container">
                            <div class="inline">
                                <span class="add_label">适用车型:</span>
                                <input id="add_cx" name="cx" class="easyui-textbox"
                                       data-options="iconCls:'',editable:true,required:true" style="width:360px" value='<?php echo ($data["cx"]); ?>'/>
                            </div>

                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="row_container">
                            <div class="inline">
                                <span class="add_label">配置:</span>
                                <input id="add_pz" name="pz" class="easyui-textbox"
                                       data-options="iconCls:'',editable:true,required:true" style="width:360px" value='<?php echo ($data["pz"]); ?>'/>
                            </div>
                        </div>
                    </td>
                </tr>
                <tr>

                    <td>
                        <div class="row_container">

                            <div class="inline">
                                <span class="add_label">分组：</span>
                                <input id="add_fz" class="easyui-combogrid" data-options="editable:true,required:false"
                                       name="fz[]" style="width:450px;" value='<?php echo ($data["fz"]); ?>'/>
                            </div>
                        </div>
                    </td>
                <tr>
                <tr>

                    <td>
                        <div class="row_container">

                            <div class="inline">
                                <span class="add_label">适用字组：</span>
                                <input  name="syfz" class="easyui-textbox"
                                       data-options="iconCls:'',multiline:true,editable:true,required:true,validType:'syfz',prompt:'只能输入英文大写字母、数字，并以英文“,”隔开！'"
                                       style="width:360px;height:80px;" value='<?php echo ($data["syfz"]); ?>'/>

                            </div>
                        </div>
                    </td>
                <tr>
                    <td>
                        <div class="row_container">
                            <div class="inline">
                                <span class="add_label">校对:</span>
                                <input id="add_s1" class="easyui-combobox" name="s1id"
                                       data-options="valueField:'value',textField:'text',url:'/solve/index.php/Admin/Public/user1',panelHeight:'auto',required:true"
                                       style="width:100px;" value='<?php echo ($data["s1id"]); ?>'/>
                            </div>

                            <div class="inline">
                                <span class="add_label">审核：</span>
                                <input id="add_s2" class="easyui-combobox" name="s2id"
                                       data-options="valueField:'value',textField:'text',url:'/solve/index.php/Admin/Public/zhuguan',panelHeight:'auto',required:true"
                                       style="width:100px;" value='<?php echo ($data["s2id"]); ?>'/>
                            </div>
                            <div class="inline">
                                <span class="add_label">批准：</span>
                                <input id="add_s3" class="easyui-combobox" name="s3id"
                                       data-options="valueField:'value',textField:'text',url:'/solve/index.php/Admin/Public/buzhang',panelHeight:'auto',required:true"
                                       style="width:100px;" value='<?php echo ($data["s3id"]); ?>'/>
                            </div>


                        </div>

                    </td>
                </tr>
                <tr>
                    <td>
                        <div class="row_container">

                            <div class="inline">
                                <span class="add_label">发放单位:</span>
                                <input id="add_ffdw" class="easyui-combogrid"
                                       data-options="editable:false,required:false,required:true" name="ffdw[]"
                                       style="width:450px;" value='<?php echo ($data["ffdw"]); ?>'/>
                            </div>

                        </div>

                    </td>
                </tr>
            </table>
            <input type="hidden" name="ids" id="ids">
            <input type="hidden" name="uid" id="userid" value=<?php echo ($_SESSION['uid']); ?>>
            <input type="hidden" name="nickname" id="nickname" value=<?php echo ($_SESSION['unickname']); ?>>
            <div style="background-color: #E0ECFF;bottom: 30px;position: absolute;width: 99.5%; ">
                <a href="#" id="" class="easyui-linkbutton" onclick='newDoc()'
                   data-options="iconCls:'icon-standard-page-word',plain:true">新建word</a>
            </div>
        </form>
    </div>

    <div data-options="region:'center',title:'office2010'" style="padding:0px;background:#eee;">


        <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome')): ?><object id='WebOffice1' type='application/x-itst-activex' align='baseline' border='0'
                    style='LEFT: 0; WIDTH: 100%; TOP: 0; HEIGHT: 100%'
                    clsid='{E77E049B-23FC-4DB8-B756-60529A35FAD5}'
                    event_NotifyCtrlReady='WebOffice1_NotifyCtrlReady'
                    codebase='./weboffice.cab#Version=7,0,0,5'>
            </object><?php endif; ?>
        <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')): ?><OBJECT id='WebOffice1' align='middle' style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%'
            classid='clsid:E77E049B-23FC-4DB8-B756-60529A35FAD5'
            event_NotifyCtrlReady='WebOffice1_NotifyCtrlReady'
            codebase='./weboffice.cab#Version=7,0,0,5'>
            </OBJECT><?php endif; ?>
        <?php if(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox')): ?><object id='WebOffice1' type='application/x-itst-activex' align='baseline' border='0'
                    style='LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%'
            clsid='{E77E049B-23FC-4DB8-B756-60529A35FAD5}'
            event_NotifyCtrlReady='WebOffice1_NotifyCtrlReady'
            codebase='./weboffice.cab#Version=7,0,0,5'>
            </object><?php endif; ?>



    </div>

</div>



<script src="/solve/Public/App/zds/word.js"></script>
<script>






    $(function(){
        var status='<?php echo ($status); ?>';
        if(status!=0){
            // 如果status不等于0,则等于ids，说明是版本升级
            $.post(APP+'/Home/Zds/findids', {
                ids : status
            }, function(data) {
                if (data.status==1) {
                    // $('#add_zds_form').form('load',data.data);
                    // $('#ffdw').combobox('setValue', data.data.ffdw);
                    // $('#add_title').textbox('setText',data.data.title);
                    // $('#add_cx').textbox('setText',data.data.cx);
                    // $('#add_pz').textbox('setText',data.data.pz);


                    // $('#cj_add').combobox('setValue',data.data.cj);
                    // setTimeout(function () {
                    //     $('#cj_add').combobox("select",data.data.cj);
                    //
                    // }, 200);

                   $('#cj_add').combobox({
                        valueField: 'id',
                        url: APP+'/Admin/Trem/cjList',
                        textField: 'title',
                        editable: false,
                        onSelect: function (rec) {
                            $('#gw_add').combogrid({
                                panelWidth: 200,
                                idField: 'title',
                                textField: 'title',
                                multiple: true,
                                striped: true,
                                url: APP+'/Admin/Trem/cjList?pid=' + rec.id,
                                columns: [[
                                    {field: 'title', title: '名称', width: 120}
                                ]]
                            });
                            $('#gw_add').combobox('clear');
                         // $('#gw_add').combogrid('setValue', data.data.gw);
                        }
                    });
                   // $('#add_fz').combogrid('setValue', data.data.fz);
                   //
                   //  $('#add_s1').combobox('select', data.data.s1);
                   //  $('#add_s2').combobox('select', data.data.s2);
                   //  $('#add_s3').combobox('select', data.data.s3);

                    // $('#add_ffdw').combogrid('setValue', data.data.ffdw);


                    // 添加一个 text
                    var idsbox="<span class='add_label'>编号：</span><input name='ids' id='add_ids' style='width:450px'/>"

                    $('#add_zds_form').append(idsbox);

                    $('#add_ids').val(data.data.ids);

                    $('#add_ids').textbox({
                        editable:false
                    });

                    // setTimeout(function () {
                    //     newDoc();
                    //
                    // }, 300);




                    // $('#add_title').textbox('setText',data.data.title);



                } else {
                    $.messager.show({ // show error message
                        title : '错误',
                        msg : data.msg
                    });
                    windowMask();

                }
            }, 'json');
        }
    })
        var webObj = document.getElementById("WebOffice1");
    function  windowMask() {
        $(".window-mask").append("<iframe style=\"position: absolute; z-index: -1; width: 100%; height: 100%; top: 0;left:0;scrolling:no;\" frameborder=\"0\"></iframe>");
    }




    /**
     * [newDoc 新建word文档]
     * @return {[type]} [description]
     */
    function newDoc() {
        // 判断form表单中是否全部写入必要内容
        var isValid = $('#add_zds_form').form('validate');
        if (!isValid) {
            $.messager.alert('警告', '请输入文档必要信息！');
            windowMask();
        } else {
            try {
                // var webObj=document.getElementById("WebOffice1");
                // 文档是否打开（是否有内容）
                var isOpen = webObj.IsOpened();
                if (isOpen > 0) {
                    var obj;
                    //获取vba document对象，以便得到content
                    obj = new Object(webObj.GetDocumentObject());

                    $.messager.confirm('确认', '当前文档非空，您确认要放弃并新建文档吗？', function (r) {
                        if (r) {
                            webObj.LoadOriginalFile("http://192.168.8.200/solve/public/word/zdsTemp.doc", "doc");
                            var obj;
                            obj = new Object(webObj.GetDocumentObject());


                            var cx = $('#add_cx').textbox('getText');
                            var pz = $('#add_pz').textbox('getText');
                            var gw = $('#gw_add').combogrid('getText');
                            var gxname = $('#add_title').textbox('getText');
                            var s1time = new Date().Format("yyyy-MM-dd");//编制日期
                            var sign = "<?php echo ($_SESSION[ 'sign']); ?>";


                            if (sign != '') {
                                // 签名到si
                                // signName('s1', sign);
                                s1 = $('#nickname').val();//编制
                                webObj.SetFieldValue("s1", s1, "");//编制
                            } else {
                                s1 = $('#nickname').val();//编制
                                webObj.SetFieldValue("s1", s1, "");//编制
                            }
                            // 如果是版本升级，南无直接写入ids
                            //     add_ids如果存在，就是版本升级

                            var status='<?php echo ($status); ?>';
                            if(status!=0){
                                webObj.SetFieldValue("ids", $('#add_ids').textbox('getText'), "");//ids
                            }
                            webObj.SetFieldValue("s1time", s1time, "");//编制日期
                            webObj.SetFieldValue("cx", cx, "");//车型
                            webObj.SetFieldValue("pz", pz, "");//配置
                            webObj.SetFieldValue("gw", gw, "");//工位
                            webObj.SetFieldValue("gxname", gxname, "");//工序名称
                            obj.ActiveWindow.View.Type = 3;
                            newSave();//保存
                        }
                    });
                    windowMask();


                } else {
                    webObj.LoadOriginalFile("http://192.168.8.200/solve/public/word/zdsTemp.doc", "doc");

                    var obj;
                    obj = new Object(webObj.GetDocumentObject());


                    var cx = $('#add_cx').textbox('getText');
                    var pz = $('#add_pz').textbox('getText');
                    var gw = $('#gw_add').combogrid('getText');
                    var gxname = $('#add_title').textbox('getText');
                    var s1time = new Date().Format("yyyy-MM-dd");//编制日期
                    var sign = "<?php echo ($_SESSION[ 'sign']); ?>";

                    if (sign != '') {
                        // 签名到si
                        // signName('s1', sign);
                        // console.log('sign:' + sign);

                        s1 = $('#nickname').val();//编制
                        webObj.SetFieldValue("s1", s1, "");//编制
                    } else {
                        s1 = $('#nickname').val();//编制
                        webObj.SetFieldValue("s1", s1, "");//编制
                    }

                    // s1 = $('#nickname').val();//编制
                    // webObj.SetFieldValue("s1", s1, "");//编制
                    // webObj.SetFieldValue("s1","http://192.168.8.200/solve/Public/images/fjp203.bmp", "::JPG::");

                    var s1time = new Date().Format("yyyy-MM-dd");//编制日期

                    var status='<?php echo ($status); ?>';
                    if(status!=0){
                        webObj.SetFieldValue("ids", $('#add_ids').textbox('getText'), "");//ids
                    }

                    // webObj.SetFieldValue("s1", s1,"");//编制
                    webObj.SetFieldValue("s1time", s1time, "");//编制日期
                    webObj.SetFieldValue("cx", cx, "");//车型
                    webObj.SetFieldValue("pz", pz, "");//配置
                    webObj.SetFieldValue("gw", gw, "");//工位
                    webObj.SetFieldValue("gxname", gxname, "");//工序名称


                    obj.ActiveWindow.View.Type = 3;


                    newSave();//保存
                }

            } catch (e) {
                $.messager.alert('异常', "异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
                windowMask();
            }
        }
    }

    /**
     * [WebOffice1_NotifyCtrlReady 初始化]
     */
    function WebOffice1_NotifyCtrlReady() {
        // 隐藏工具栏
        webObj.ShowToolBar = false;
        UnProtect();//解除文档保护

    }

    /**
     * [submit_zds 保存或者发布作业指导书]
     * @param  {[type]} para [0或者1，保存或者发布]
     * @return {[type]}      [description]
     */
    function submit_zds(para) {
        var isValid = $('#add_zds_form').form('validate');
        if (!isValid) {
            $.messager.alert('警告', '请输入文档必要信息！');
            windowMask();
        } else {
            // 文档是否打开（是否有内容）
            var isOpen = webObj.IsOpened();
            var obj;
            obj = new Object(webObj.GetDocumentObject());
            if (isOpen > 0) {
                // 1、获取ids
                // 1、thinkphp from 方式写库,返回ids
                $('#add_zds_form').form('submit', {
                    url: APP+'/Home/Zds/add',
                    onSubmit: function (param) {   //param为保存或者发布
                        param.status = para;
                        param.content = obj.Content.Text; //文档内容
                    },
                    success: function (data) {
                        data = $.parseJSON(data);

                        if (data.status == 1) {
                            try {
                                var returnValue;

                                webObj.HttpInit();

                                //初始化Http引擎我测试一下试
                                // 添加相应的Post元素
                                // if($("#add_ids").length > 0){
                                //     var  ids=$('#add_ids').textbox('getText');
                                //     if(webObj.GetFieldValue('ids','')==''){
                                //         webObj.SetFieldValue("ids",  ids, "");//ids
                                //     }
                                //     webObj.HttpAddPostString("ids", ids);
                                // }else{
                                //     if (para == 'will_check') {
                                //         webObj.HttpAddPostString("ids", data.ids);
                                //         UnProtect();
                                //         webObj.SetFieldValue('ids', data.ids, "");//编制
                                //         ProtectFull();
                                //     } else {
                                //         webObj.HttpAddPostString("ids", '');
                                //         UnProtect();
                                //         webObj.SetFieldValue('ids', '', "");//编制
                                //         ProtectFull();
                                //     }

                                // }


                                webObj.HttpAddPostString("ids", '');
                                webObj.HttpAddPostString("ida", data.id);
                                webObj.HttpAddPostString("title", $('#add_title').textbox('getText'));
                                webObj.HttpAddPostString("DocType", "doc");

                                webObj.HttpAddPostCurrFile("DocContent", "");

                                newSave();//保存

                                returnValue = webObj.HttpPost('http://192.168.8.200/wordupload/savedoc.php?number=Math.random()');  // 判断上传是否成功
                                console.log(returnValue);

                                if (returnValue == "succeed") {

                                    $.messager.confirm('确认', '您想要返回列表页面吗？', function (r) {
                                        if (r) {
                                            var tab = $('#tab').tabs('getSelected');
                                            if ($('#WebOffice1')) {
                                                $('#WebOffice1').remove();
                                            }
                                            //encodeURI() 函数可把字符串作为 URI 进行编码
                                            tab.panel('refresh', APP + '/Home/Zds/showEditor');
                                        }
                                    });
                                    windowMask();
                                } else if (returnValue == "failed")

                                    $.messager.alert('失败3', '文件上传失败！');
                                windowMask();

                            } catch (e) {
                                $.messager.alert('异常2', "异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
                                windowMask();
                            }

                        } else {
                            $.messager.alert('失败1', data.msg, 'info');
                            windowMask();

                        }

                    }
                });

            } else {
                $.messager.alert('警告', 'word没有写入内容！', 'info');
                windowMask();
            }
        }

    }

    function closeDoc() {
        try {
            var webObj = document.getElementById("WebOffice1");
            webObj.CloseDoc(1);
        } catch (e) {
            $.messager.alert('异常',"异常\r\nError:"+e+"\r\nError Code:"+e.number+"\r\nError Des:"+e.description);
            windowMask();
        }

    }

    // 打开本地文件
    function docOpen() {


        if ($('#title').textbox('getText') == '') {
            // $.messager.alert('警告','请输入文件名称');

            $.messager.alert('警告', '请输入文件名称！', 'info');
            windowMask();
            $('#title').focus();
            return false;
        }
        if (0 == document.all.WebOffice1.LoadOriginalFile(myform.DocFilePath.value, "doc")) {
            alert("文件打开失败，请检查路径是否合法");
            myform.DocFilePath.focus();
            return false;
        }
    }


    var cj = $('#cj_add').combobox({
        valueField: 'id',
        url: APP+'/Admin/Trem/cjList',
        textField: 'title',
        editable: false,
        onSelect: function (rec) {
            $('#gw_add').combogrid({
                panelWidth: 200,
                idField: 'title',
                textField: 'title',
                multiple: true,
                striped: true,
                url: APP+'/Admin/Trem/cjList?pid=' + rec.id,
                columns: [[
                    {field: 'title', title: '名称', width: 120}
                ]]
            });
            $('#gw_add').combobox('clear');
            // $('#gw_add').combogrid('setValue', '');
        }
    });


    $('#add_ffdw').combogrid({

        panelWidth: 450,
        idField: 'dwname',
        textField: 'dwname',
        multiple: true,
        delay: 500,
        mode: 'remote',
        striped: true,
        url: APP + '/Home/Word/ffdwList',
        columns: [[
            {field: 'dwname', title: '单位', width: 120},
            {
                field: 'fzr', title: '负责人', width: 300,
                formatter: function (value, row, index) {
                    // console.log(value);
                    var str = "";
                    if (value.length != 0) {
                        var data = $.parseJSON(value);

                        for (var i = 0; i < data.length; i++) {
                            str = str + data[i].name + '(' + data[i].email + ')';
                            if (i != data.length - 1) {
                                str = str + ',';
                            }
                        }


                    } else {
                        str = "";
                    }
                    return str;

                }
            }
        ]],
        fitColumns: false
    });

    $('#add_fz').combogrid({

        panelWidth: 450,
        idField: 'dc',
        textField: 'dc',
        multiple: true,
        delay: 500,
        mode: 'remote',
        striped: true,
        url: APP + '/Admin/Trem/fzhList',
        columns: [[
            {field: 'zc', title: '总成', width: 120},
            {field: 'dc', title: 'dc', width: 60},
            {field: 'fzname', title: '分组名称', width: 180},
            {field: 'sm', title: '分组主要零部件', width: 200},
            {field: 'fzr', title: '负责人', width: 80}
        ]],
        fitColumns: false
    });
    $('#fz_edit').combogrid({

        panelWidth: 640,
        idField: 'dc',
        textField: 'dc',
        multiple: true,
        delay: 500,
        mode: 'remote',
        striped: true,
        url: APP+'/Admin/Trem/fzhList',
        columns: [[
            {field: 'zc', title: '总成', width: 120},
            {field: 'dc', title: 'dc', width: 60},
            {field: 'fzname', title: '分组名称', width: 180},
            {field: 'sm', title: '分组主要零部件', width: 200},
            {field: 'fzr', title: '负责人', width: 80}
        ]],
        fitColumns: true
    });
    $('#ffz_edit').combogrid({

        panelWidth: 640,
        idField: 'dc',
        textField: 'dc',
        multiple: false,
        delay: 500,
        mode: 'remote',
        striped: true,
        url: APP+'/Admin/Trem/fzhList',
        columns: [[
            {field: 'zc', title: '总成', width: 120},
            {field: 'dc', title: 'dc', width: 60},
            {field: 'fzname', title: '分组名称', width: 180},
            {field: 'sm', title: '分组主要零部件', width: 200},
            {field: 'fzr', title: '负责人', width: 80}
        ]],
        fitColumns: true
    });


    function go_back_zds(id) {
        $.messager.confirm('确认','返回前需要保存吗？',function(r){
            if (r){
                var s = savezds(id);
            }
        });
        windowMask();
        setTimeout(function () {
            var tab = $('#tab').tabs('getSelected');
            if ($('#WebOffice1')) {
                $('#WebOffice1').remove();
            }
//encodeURI() 函数可把字符串作为 URI 进行编码
            tab.panel('refresh', APP+'/Home/Zds/showEditor');
        }, 2000);

    }

</script>